function http(url, method, data = {}, header = {}, config = {}){
    return new Promise((resolve, reject) => {
        // 默认显示loading
        let showLoading = true;
        if (config['showLoading'] != undefined && config['showLoading'] === false) {
            showLoading = false;
        }
        // 是否显示loading
        if (showLoading) {
            wx.showLoading({
                title: "", icon: 'none', mask: true 
            });
        }  
        wx.request({
            url: url,
            method: method,
            data: data,
            header: header,
            success: (res => {
                if (showLoading) {
                    wx.hideLoading();
                }   
                if(res.data.code == 'CRM-SESSION-TIMEOUT'){
                    wx.showToast({
                      title: res.data.msg,
                      icon:'none'
                    })
                    setTimeout(() => {
                        wx.navigateTo({
                          url: '../login/login',
                        })
                    }, 2000);
                    return;
                }                             
                resolve(res.data);
            }),
            fail: (err => {
                if (showLoading) {
                    wx.hideLoading();
                }
                reject(err);
            })
        });
    });
}
let baseUrl = 'https://www.eastcoms.com';  //云平台端口
// get 网络请求
function getRequest(url, data = {}, header = {}, config = {}){
    console.log(url)
    return http(baseUrl+url, "GET", data,  header, config);
  }
  
// post 不带header网络请求
  function postNoToken(data){
    let params = {
        timeStamp: getNowFormatDate(),
        apiCode: data.apiCode,
        appId: "APP_H5",
        sign: "",
        params: data.params,
        token: ""
      }
    let header = {
        "Content-Type": data.header ? data.header :"application/json"
    };
    return http(baseUrl, "POST", params, header);
  }
// post 带header网络请求
function post(data){
    // 用户token
    let params = {
        timeStamp: getNowFormatDate(),
        apiCode: data.apiCode,
        appId: "APP_H5",
        sign: "",
        params: data.params,
        token: wx.getStorageSync("info").token
      }
    let header = {
        "Content-Type": data.header ? data.header :"application/json"
    };
    return http(baseUrl, "POST", params, header);
}
function getNowFormatDate(){
    var date = new Date();
    var seperator1 = "-";  
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var hour = date.getHours();
    var minutes = date.getMinutes() ;
    var seconds = date.getSeconds();
    var strDate = date.getDate();    
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate+" "+hour+":" +minutes+":" +seconds;  
    return currentdate;
}
module.exports ={
    getRequest:getRequest,
    postNoToken:postNoToken,
    post:post
}